package com.awanp3.nimbus.base.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.awanp3.nimbus.common.idempotent.annotation.RepeatSubmit;
import com.awanp3.nimbus.common.log.annotation.Log;
import com.awanp3.nimbus.common.web.core.BaseController;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.awanp3.nimbus.common.core.domain.R;
import com.awanp3.nimbus.common.core.validate.AddGroup;
import com.awanp3.nimbus.common.core.validate.EditGroup;
import com.awanp3.nimbus.common.log.enums.BusinessType;
import com.awanp3.nimbus.common.excel.utils.ExcelUtil;
import com.awanp3.nimbus.base.domain.vo.BaseCompanyVo;
import com.awanp3.nimbus.base.domain.bo.BaseCompanyBo;
import com.awanp3.nimbus.base.service.IBaseCompanyService;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;

/**
 * 公司
 * 前端访问路由地址为:/base/company
 *
 * @author 20014170
 * @date 2025-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/company")
public class BaseCompanyController extends BaseController {

    private final IBaseCompanyService baseCompanyService;

    /**
     * 查询公司列表
     */
    @SaCheckPermission("base:company:page")
    @GetMapping("/page")
    public TableDataInfo<BaseCompanyVo> page(BaseCompanyBo bo, PageQuery pageQuery) {
        return baseCompanyService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询公司列表
     */
    @SaCheckPermission("base:company:list")
    @GetMapping("/list")
    public R<List<BaseCompanyVo>> list(BaseCompanyBo bo) {
        return R.ok(baseCompanyService.queryList(bo));
    }

    /**
     * 导出公司列表
     */
    @SaCheckPermission("base:company:export")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseCompanyBo bo, HttpServletResponse response) {
        List<BaseCompanyVo> list = baseCompanyService.queryList(bo);
        ExcelUtil.exportExcel(list, "公司", BaseCompanyVo.class, response);
    }

    /**
     * 获取公司详细信息
     *
     * @param companyId 主键
     */
    @SaCheckPermission("base:company:query")
    @GetMapping("/{companyId}")
    public R<BaseCompanyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("companyId") Long companyId) {
        return R.ok(baseCompanyService.queryById(companyId));
    }

    /**
     * 新增公司
     */
    @SaCheckPermission("base:company:add")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseCompanyBo bo) {
        return toAjax(baseCompanyService.insertByBo(bo));
    }

    /**
     * 修改公司
     */
    @SaCheckPermission("base:company:edit")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseCompanyBo bo) {
        return toAjax(baseCompanyService.updateByBo(bo));
    }

    /**
     * 删除公司
     *
     * @param companyIds 主键串
     */
    @SaCheckPermission("base:company:remove")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{companyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("companyIds") Long[] companyIds) {
        return toAjax(baseCompanyService.deleteWithValidByIds(List.of(companyIds), true));
    }
}
